package com.atguigu.gmall.user.controller;

import cn.jiguang.common.ClientConfig;
import cn.jiguang.common.resp.APIConnectionException;
import cn.jiguang.common.resp.APIRequestException;
import cn.jpush.api.JPushClient;
import cn.jpush.api.push.PushResult;
import cn.jpush.api.push.model.Platform;
import cn.jpush.api.push.model.PushPayload;
import cn.jpush.api.push.model.audience.Audience;
import cn.jpush.api.push.model.audience.AudienceTarget;
import cn.jpush.api.push.model.notification.Notification;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;

import static cn.jpush.api.push.model.notification.PlatformNotification.ALERT;

@Controller
public class PushController {


    @RequestMapping("pushUsers")
    @ResponseBody
    public String tagPush(@RequestParam Map<String, String> params) {
        //蜗牛
        JPushClient jpushClient = new JPushClient("5f5ce90d7af0ef3d06b5265b", "f5eba399005f4c8d95894eca", null, ClientConfig.getInstance());
        //测试
        //JPushClient jpushClient = new JPushClient("ffe3e8ca250f8664746b6f8d", "ea4eb97f219eb395134f3a24", null, ClientConfig.getInstance());
        Map<String, String> pushTag = new HashMap<>();
        pushTag.put("type", params.get("type"));
        pushTag.put("url", params.get("url"));

        //PushPayload payload = buildtagPush(pushTag, params.get("title"), params.get("content"), params.get("target"));
        PushPayload payload = build_tag(pushTag, params.get("title"), params.get("content"));

        String resultStr = "";

        try {
            PushResult result = jpushClient.sendPush(payload);
            //result.
            resultStr = result.getOriginalContent();
            jpushClient.close();

        } catch (APIConnectionException e) {
            // Connection error, should retry later

        } catch (APIRequestException e) {
            // Should review the error, and fix the request
        }

        return resultStr;
    }

    @RequestMapping("pushUser")
    @ResponseBody
    public String Push(@RequestParam Map<String, String> params) {
        //蜗牛
        JPushClient jpushClient = new JPushClient("5f5ce90d7af0ef3d06b5265b", "f5eba399005f4c8d95894eca", null, ClientConfig.getInstance());
        //测试
        //JPushClient jpushClient = new JPushClient("ffe3e8ca250f8664746b6f8d", "ea4eb97f219eb395134f3a24", null, ClientConfig.getInstance());
        Map<String, String> pushTag = new HashMap<>();
        pushTag.put("type", params.get("type"));
        pushTag.put("url", params.get("url"));

        //PushPayload payload = buildtagPush(pushTag, params.get("title"), params.get("content"), params.get("target"));
        PushPayload payload = build_alias(params.get("user"),pushTag, params.get("title"), params.get("content"));

        String resultStr = "sss";

        try {
            PushResult result = jpushClient.sendPush(payload);
            //result.
            resultStr = result.getOriginalContent();
            jpushClient.close();

        } catch (APIConnectionException e) {
            // Connection error, should retry later

        } catch (APIRequestException e) {
            // Should review the error, and fix the request
        }

        return resultStr;
    }


    public PushPayload build_tag(Map<String,String> params,String title,String content) {
        return PushPayload.newBuilder()
                .setPlatform(Platform.android())
                .setAudience(Audience.tag("test"))
                .setNotification(Notification.android(content, title, params))
                .build();
    }

    public static PushPayload build_alias(String alias,Map<String,String> params,String title,String content) {
        return PushPayload.newBuilder()
                .setPlatform(Platform.all())
                .setAudience(Audience.alias(alias))
                .setNotification(Notification.android(content, title, params))
                .build();
    }

    public PushPayload buildtagPush(Map<String, String> params, String title, String content, String tag) {
        return PushPayload.newBuilder()
                .setPlatform(Platform.android())
                .setAudience(Audience.tag("test"))
                .setNotification(Notification.android(content, title, params))
                //.setNotification(Notification.ios()
                .build();
    }

    public PushPayload buildPushObject_android_tag_alertWithTitle() {
        Map<String, String> params = new HashMap<>();
        params.put("key", "value");
        return PushPayload.newBuilder()
                .setPlatform(Platform.android())
                //.setAudience(Audience.tag("vip","tag1"))//客户端包含其中一个即可收到推送
                //.setAudience(Audience.tag_and("vip","tag1"))//客户端包含所有tag才能收到推送
                //.setAudience(Audience.tag_not("vip"))//客户端不包含此tag才能收到推送
                .setAudience(
                        Audience.newBuilder()
                                .addAudienceTarget(AudienceTarget.tag("vip"))
                                //.addAudienceTarget(AudienceTarget.tag("s"))//增加同种tag会覆盖上面的条件
                                //.addAudienceTarget(AudienceTarget.tag_and("n","m"))
                                //.addAudienceTarget(AudienceTarget.tag("vip"))
                                .build()
                )
                //.setAudience(Audience.tag("vip","tag1"))
                //.setAudience()
                .setNotification(Notification.android("content", "title", params))
                //.setNotification(Notification.ios()
                .build();
    }

    public PushPayload buildPushObject_all_all_alert() {
        return PushPayload.alertAll(ALERT);
    }
}
